package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;



@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public R<Employee> login(HttpServletRequest res, Employee employee) {
//①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//②. 根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = this.getOne(lqw);
//③. 如果没有查询到, 则返回登录失败结果
        if (emp==null) {
            return R.error("登录失败");
        }
//④. 密码比对，如果不一致, 则返回登录失败结果
        if (!password.equals(emp.getPassword())) {
            return R.error("登录失败");
        }
//⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus()==0) {
            return R.error("员工已禁用");
        }
//⑥. 登录成功，将员工id存入Session, 并返回登录成功结果
        res.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @Override
    public R<String> logout(HttpServletRequest res) {
        //清理Session中保存的当前登录员工的id
        res.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @Override
    public R<Page> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        //A. 构造分页条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        //B. 构建搜索条件 - name进行模糊匹配
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //C. 构建排序条件 - 更新时间倒序排序
        lqw.orderByDesc(Employee::getUpdateTime);
        //D. 执行查询
        this.page(pageInfo, lqw);
        //E. 组装结果并返回
        return R.success(pageInfo);
    }

    @Override
    public R<String> save(HttpServletRequest request, Employee employee) {
        //A. 在新增员工时， 按钮页面原型中的需求描述， 需要给员工设置初始默认密码 123456， 并对密码进行MD5加密。
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        //B. 在组装员工信息时, 还需要封装创建时间、修改时间，创建人、修改人信息(从session中获取当前登录用户)。
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //获得当前登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        this.save(employee);
        return R.success("新增员工成功");
    }

    @Override
    public R<String> update(HttpServletRequest request, Employee employee) {

//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        this.updateById(employee);

        return R.success("员工信息修改成功");
    }

    @Override
    public R<Employee> selectById(Serializable id) {
        Employee employee = this.getById(id);
        if (employee!=null) {
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
